package com.emily.test.service.impl;

import com.emily.test.mapper.TDeptMapper;
import com.emily.test.model.TDept;
import com.emily.test.service.DeptService;
import com.emily.test.utils.ConstentUtil;
import com.emily.test.utils.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.*;

/**
 * @author ：zc
 * @date ：Created in 2019/3/27 10:37
 * @description：
 */
@Service
@Transactional(rollbackFor = {Exception.class})
public class DeptServiceImpl implements DeptService {
    @Resource
    TDeptMapper tDeptMapper;

    @Override
    public List<TDept> getDeptAllList() {
        return tDeptMapper.getDeptAllList();
    }

    @Override
    public List<TDept> getDeptList(String id) throws Exception {
        return tDeptMapper.getDeptList(id);
    }

    @Override
    public List<Map<String, Object>> getDeptMapList(String code) throws Exception {

        List<Map<String, Object>> mapList= tDeptMapper.getDeptMapList(code);
        if(!ConstentUtil.isEmpty(mapList)){
            for(Map map :mapList){
                Integer pid= (Integer) map.get("pid");
                map.put("pName","");
                if(!ConstentUtil.isEmpty(pid)){
                    for(Map maps :mapList){
                        Integer id= (Integer) maps.get("id");
                        if(pid.equals(id)){
                            map.put("pName",maps.get("deptName"));
                            break;
                        }
                    }
                }
            }
        }
        return mapList;
    }

    @Override
    public List<Map<String, Object>> getDeptMapListToTree(String code) throws Exception {
        List<Map<String, Object>> mapList= getDeptMapList(code);
        List<Map<String, Object>> mapLists= new ArrayList<Map<String, Object>>();
        for(Map map : mapList){
             Map<String,Object> ma=new HashMap<String,Object>();
             ma.put("id",map.get("id"));
             ma.put("name",map.get("deptName"));
             ma.put("pId",map.get("pid"));
             mapLists.add(ma);
        }
        return mapLists;
    }
//递归查询实例
//    public List<Map<String,Object>> getAllDeptList(List<Map<String,Object>> allDept,List<Map<String,Object>> returnDeptList){
//        for (Map<String,Object> deptMap: allDept) {
//            //获取用户的部门信息
//            String deptName=deptMap.get("deptName")==null? "" : (String)deptMap.get("deptName");
//            String id=deptMap.get("id")==null? "" : (String) deptMap.get("id");
//            Map<String,Object> up=new HashMap<String, Object>();
//            up.put("id",id);
//            up.put("deptName",deptName);
//            up.put("pid","");
//            returnDeptList.add(up);
//            if(!deptMap.containsKey("children")){
//                continue;
//            }
//            List<Map<String,Object>> childrenList=(List<Map<String,Object>>)deptMap.get("children");
//            getAllDeptLists(childrenList,returnDeptList,deptCode);
//
//        }
//        return returnDeptList;
//    }
//
//    public List<Map<String,Object>> getAllDeptLists(List<Map<String,Object>> allDept,List<Map<String,Object>> returnDeptList,String parentDeptCode) throws TZException{
//        for (Map<String,Object> deptMap: allDept) {
//            //获取用户的部门信息
//            String dept=deptMap.get("text")==null? "" : (String)deptMap.get("text");
//            String deptCode=deptMap.get("id")==null? "" : (String) deptMap.get("id");
//            Map<String,Object> up=new HashMap<String, Object>();
//            up.put("deptCode",deptCode);
//            up.put("dept",dept);
//            up.put("parentDeptCode",parentDeptCode);
//            up.put("total",0);
//            returnDeptList.add(up);
//            if(!deptMap.containsKey("children")){
//                continue;
//            }
//            List<Map<String,Object>> childrenList=(List<Map<String,Object>>)deptMap.get("children");
//            getAllDeptLists(childrenList,returnDeptList,parentDeptCode);
//
//        }
//        return returnDeptList;
//    }

    @Override
    public TDept getDept(Integer id) throws Exception {
        return tDeptMapper.getDept(id);
    }

    @Override
    public void delete(Integer id) throws Exception {
        tDeptMapper.delDept(id);
    }

    @Override
    public int insert(TDept o) {
        o.setUpdateTime(DateUtil.getNowStr());
        String deptCode;
        TDept num = new TDept();
        System.out.println(num);
        do {
            deptCode = o.getCompanyCode() +"-"+ new Random().nextInt((999999 - 111111 + 1));
            num  = tDeptMapper.selectDeptByCode(deptCode);
        } while (num!=null);
        o.setDeptCode(deptCode);
        return tDeptMapper.insertSelective(o);
    }

    @Override
    public void update(TDept o) {
        o.setUpdateTime(DateUtil.getNowStr());
        tDeptMapper.update(o);
    }
}
